const { sqlConnect }= require('../modules/mysql')
// https://github.com/thomas-alrek/node-php-password
const Password = require("node-php-password"); // 密码哈希
const { gernerateRandomCode }  = require('../modules/util')
const client = require('../modules/redis')
const nodemailer = require('../modules/nodemailer')


//发送验证码
const sendEmail = async (req,res) => {
    // 生辰4位随机验证码
    let code = gernerateRandomCode(4)
    const mail = req.body.mail
     // 存入redis,
    client.set(mail, code).then(response => {
       // 设置成功发送邮件
        nodemailer(mail, code)
        res.sendResult(null, 200, '验证码发送成功')
    })
    //设置过期时间 60s
    client.expire(mail, 60);
}
// 验证码登录
const codeLogin = async (req, res) => {
    const { mail, code } = req.body
    // 从redis查询
    client.get(mail).then(localCode => {
        if (code === localCode) {
            console.log('验证成功')
            res.sendResult()
        } else {
            console.log('验证失败、是否过期')
            res.sendResult()
        }
    }) 
}

// PC端 用户名、密码 登录
const pcLogin = async (username, password, cb) => {
//    console.log(username,password, Password.hash(password))
   let sql = `select * from sp_manager where mg_name='${username}'`
   let sqlArr = []
   let callback =  (err, data) => {
        if(err) {
            console.log('连接出错了')
        } else {
            // console.log(data[0],'data')
            const manager = data[0]
            if (!data.length) {
               return  cb('该用户不存在!')
            }
            if (manager.role_id < 0) {
               return  cb('该用户没有登录权限')
            }
            if (manager.role_id !== 0 && manager.mg_state !== 1) {
               return  cb('该用户已经被禁用')
            }
            if(Password.verify(password,Password.hash(password))) {
                cb(null, {
                    // 返回的数据
                    id: manager.mg_id,
                    rid: manager.role_id,
                    username: manager.mg_name,
                    mobile: manager.mg_mobile,
                    email: manager.mg_email
                })
            } else {
                return cb('密码错误')
            }
        }
    }
    await sqlConnect(sql, sqlArr, callback)
}
// 获取所有用户
const getAllUsers = async(req, res) => {
    console.log('qqq')
    let sql = 'select * from sp_manager'
    let sqlArr = []
    let callback =  (err, data) => {
        if(err) {
            console.log('连接出错了')
        } else {
            res.send({
                'user': data
            })
        }
    }
    await sqlConnect(sql, sqlArr, callback)
}
// 获取左侧菜单
const getLeftMenus = async(req, res) => {
    let userInfo = req.userInfo
    if (!userInfo) res.sendResult(null,400,'无权限访问')
    let sql = 'select * from sp_permission_api as api left join sp_permission as main on main.ps_id = api.ps_id where main.ps_id is not null'
    let sqlArr = []
    let callback = (err, data) => {
        if (err) {
            console.log('连接出错了')
        } else {
            console.log(data)
        }
    }
    await sqlConnect(sql,sqlArr,callback)
}

module.exports = {
    getAllUsers,
    pcLogin,
    sendEmail,
    codeLogin,
    getLeftMenus
}